import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/api.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/detail/sections/entrance_guard_info.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/detail/sections/operation.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Detail extends StatefulWidget {
  final int faceLockId;
  const Detail({super.key, required this.faceLockId});

  @override
  State<Detail> createState() => _DetailState();
}

class _DetailState extends State<Detail> {

  Map? _info;

  void _getFaceLockInfo() async {
    Map resData = await entranceGuardApi.getFaceLockInfo(widget.faceLockId);
    print(resData);
    setState(() {
      _info = resData["data"];
    });
  }


  @override
  void initState() {
    _getFaceLockInfo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("门禁", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(horizontal: ThemeVars.paddingPage, vertical: 20),
        children: [
          if (_info != null) EntranceGuardInfo(info: _info!),
          const SizedBox(height: 15,),
          Operation(info: _info),
          const SizedBox(height: 15,),
          Button(
            onPressed: () async {
              await entranceGuardApi.unlock(_info?["faceLockId"]);
              EasyLoading.showSuccess("开锁成功");
            },
            child: const Text("远程开锁")
          ),
        ],
      ),
    );
  }

}